import load from '../templates/loader/loader.js'
import api from '../../api/api.js'
var requestParam = {
  REQ_HEAD: {
    SIGN: "8495897b17c2813da1ef5e8c7ca45e21"
  },
  REQ_BODY: {
    goodName: "",
    pSize: 10,
    pNum: 1,
  }
}
Page({
  data: {
    searchActive: false,
    modal: {
      title: "众筹说明",
      content: "在有限时间内整合众人需求，达到成交总量要求，即为众筹成功。若成功，则返回零售价差款。若失败，则按零售价安排发货，也可申请退款。不同厂商，众筹时间和总量各不相同。",
      open: false,
    },
    itemData: [],
  },
  onLoad: function () {
    var that = this
    that.loader = load.loader(that, {
      size: 50,
      borderWidth: 3,
    });
    that.loaderSamll = load.loader(that, {
      data: "loaderSamll",
      toast: false,
    });
    that.loader.open();
    that.loadData();
  },
  loadData: function (sgin, callback) {
    var that = this
    api.getCrowdfunding({
      method: 'POST',
      data: {
        REQ_MESSAGE: JSON.stringify(requestParam)
      },
      success: (res) => {
        that.loader.close();
        if (sgin == 1) {
          that.setData({
            itemData: ""
          })
        }
        if (!res.rows) {
          if (sgin == 2) {
            that.loaderSamll.open("没有更多了");
          } else {
            that.loader.open("没有更多了");
          }
          return
        }
        if (res.rows.length <= 0) {
          if (sgin == 2) {
            that.loaderSamll.open("没有更多了");
          } else {
            that.loader.open("没有更多了");
          }
        } else {
          requestParam.REQ_BODY.pNum++;
          res.rows.forEach(function (item) {
            if (item.start_time) {
              item.create_time = that.dateFtt("yyyy-MM-dd hh:mm:ss", new Date(item.create_time));
            }
          });
          that.data.itemData.length <= 0 ? that.data.itemData = res.rows : that.data.itemData = that.data.itemData.concat(res.rows)
          that.setData({
            itemData: that.data.itemData
          })
        }
      },
      fail: (res) => {
        that.loader.open(api.statusFail.title);
      },
      complete: (res) => {
        if (typeof callback == "function") {
          callback();
        }
      }
    });
  },
  searchFocus: function () {
    this.setData({
      searchActive: true
    })
  },
  searchBlur: function (e) {
    if (e.detail.value == "") {
      this.setData({
        searchActive: false
      })
    }
  },
  search: function (e) {
    requestParam.REQ_BODY.goodName = e.detail.value;
    requestParam.REQ_BODY.pSize = 10;
    requestParam.REQ_BODY.pNum = 1;
    this.loaderSamll.close()
    this.loadData(1);
  },
  open_modal: function () {
    this.data.modal.open = true
    this.setData({
      modal: this.data.modal
    })
  },
  close_modal: function () {
    this.data.modal.open = false
    this.setData({
      modal: this.data.modal
    })
  },
  confirm_select: function () {
    this.close_modal();
  }, //下拉刷新
  onPullDownRefresh: function () {
    var that=this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    requestParam.REQ_BODY.goodName = "";
    requestParam.REQ_BODY.pNum = 1;
    this.loadData(1, function () {
      that.loaderSamll.close();
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
    setTimeout(function () {
      that.loaderSamll.close();
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 10000);
  },
  onReachBottom: function () {
    this.loaderSamll.open();
    this.loadData(2);
  },
  dateFtt: function (fmt, date) {
    var o = {
      "M+": date.getMonth() + 1, //月份
      "d+": date.getDate(), //日
      "h+": date.getHours(), //小时
      "m+": date.getMinutes(), //分
      "s+": date.getSeconds(), //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }
})